$(function () {
    var empLossdata=$("#empLossdata")
    var tabsForm=$("#tabsForm")
    empLossdata.datagrid({
        url:"/studentmanagement/lossList.do",
        fit:true,
        fitColumns:true,
        striped:true,
        sortName:"lossDate",//默认排序规则
        sortOrder:"desc",
        pagination:true,
        columns:[[
            {field:"id",checkbox:true},
            {field:"name",title:"姓名",width:80},//姓名
            {field:"tel",title:"电话",width:80},//电话
            {field:"className",title:"班级",width:80},//班级
            {field:"phase",title:"流失阶段",width:80},//流失阶段
            {field:"info",title:"流失原因",width:80},//流失原因
            {field:"lossDate",title:"流失时间",width:80,sortable:true},//流失时间
            {field:"refund",title:"是否退款",width:80,formatter: function (value) {
                    return value?'是':"否"
                }},//是否退款
            {field:"salesman",title:"营销人员",width:100,formatter: function (value) {
                    return value.username
                }},//营销人员
            {field:"handle",title:"经办人",width:80,formatter: function (value) {
                    return value.username
                }},//经办人
            {field:"state",title:"审核状态",width:80,formatter: function (value) {
                    return value?'<font color="green">已审核</font>':'<font color="red">未审核</font>'
                }},//审核状态
        ]],
        toolbar:"#buttons",
        onSelect:function () {
            var row=empLossdata.datagrid("getSelected");
            if(row.state){
                $("#audit").linkbutton("disable")
                $("#edit").linkbutton("disable")
            }else{
                $("#audit").linkbutton("enable")
                $("#edit").linkbutton("enable")
            }
        }
    })

    var methodObj={
        edit:function () {
            var row=empLossdata.datagrid("getSelected");
            if(row) {
                $("#myDialog").dialog({
                    title: '修改'
                })
                if(row.salesman){
                    row["salesman.username"]=row.salesman.username
                }
                if(row.handle){
                    row["handle.username"]=row.handle.username
                }
                $("#myDialog").dialog("open")
                tabsForm.form("load",row)
                tabsForm.form(row)
            }
        },
        refresh:function () {
            empLossdata.datagrid("reload")
        },
        query:function () {
            var keeyWord=$("#keyWord").textbox("getValue")
            var startDate=$("#startDate").datebox("getValue")
            var stopDate=$("#stopDate").datebox("getValue")
            var keyClassName=$("#keyClassName").combobox("getValue");
            empLossdata.datagrid("load",{
                keyWord:keeyWord,
                startDate:startDate,
                stopDate:stopDate,
                keyClassName:keyClassName
            })
        },
        out:function () {
            var keyWord=$("#keyWord").textbox("getValue")
            var startDate=$("#startDate").textbox("getValue")
            var stopDate=$("#stopDate").textbox("getValue")
            var keyClassName=$("#keyClassName").textbox("getValue")
            window.open("/export/lossExportXLS.do?keyWord="+keyWord+"&startDate="+startDate+
                "&stopDate="+stopDate+"&keyClassName="+keyClassName)
        },
        audit:function () {
            var row=empLossdata.datagrid("getSelected");
            $.post("/studentmanagement/audit.do",{"id":row.id},function (data) {
                if(data.success){
                    alert("审核成功")
                }else{
                    alert("审核失败")
                }
                empLossdata.datagrid("reload")
            },"json")
        },
        submit:function () {
            tabsForm.form("submit",{
                url:"/studentmanagement/updateLoss.do",
                success:function (data) {
                    data=$.parseJSON(data)
                    if(data.success){
                        $.messager.alert("温馨提示","编辑成功","info",function () {
                            methodObj.cancel();
                            empLossdata.datagrid("reload");
                        })
                    }else{
                        $.messager.alert("温馨提示",data.msg,"error");
                    }
                }
            })
        },
        cancel:function() {
            $("#myDialog").dialog("close");
        },
        reverse:function () {
            var row=empLossdata.datagrid("getSelected");
            $.post("/studentmanagement/reverse.do",{"id":row.id},function (data) {
                if(data.success){
                    alert("操作成功")
                }else{
                    alert("操作失败")
                }
                empLossdata.datagrid("reload")
            },"json")
        }
    }
    $("#myDialog").dialog({
        closed:true,
        width:400,
        height:350,
        onColse:function () {
            tabsForm.form("clear")
        },
        buttons:$("#btns")
    })
    $("a[data-click]").click(function () {
        var incident=$(this).data("click");
        methodObj[incident]();
    })
})